package com.qingkai.onlineshop.presentation.dashboard

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.graphics.drawable.toBitmapOrNull
import androidx.palette.graphics.Palette
import coil3.asDrawable
import coil3.compose.AsyncImage
import com.qingkai.onlineshop.R
import com.qingkai.onlineshop.data.entity.CategoryEntity
import com.qingkai.onlineshop.data.entity.ItemEntity
import com.qingkai.onlineshop.data.entity.PicUrlEntity
import com.qingkai.onlineshop.data.relations.ItemFull
import com.qingkai.onlineshop.data.relations.ItemWithPics
import com.qingkai.onlineshop.utils.toNonHardwareBitmap
import kotlinx.coroutines.launch

/**
 *作者：created by 89270 on 2025/6/10 09:34
 *描述：
 */
@Composable
fun ProductItem(modifier: Modifier = Modifier, itemWithPics: ItemWithPics) {
    val scope = rememberCoroutineScope()
    val context = LocalContext.current
    val defaultColor = MaterialTheme.colorScheme.primaryContainer
    var dominantColor by remember { mutableStateOf(defaultColor) }

    Box(modifier = modifier, contentAlignment = Alignment.Center) {
        Box(
            modifier
                .fillMaxWidth()
                .height(160.dp)
                .clip(RoundedCornerShape(16.dp))
                .background(MaterialTheme.colorScheme.primaryContainer)
                .align(alignment = Alignment.BottomCenter)
        )
        Column(
            modifier = Modifier.padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            AsyncImage(
                model = itemWithPics.pics.firstOrNull()?.url ?: "",
                contentDescription = "product-${itemWithPics.item.title}",
                error = painterResource(android.R.drawable.ic_menu_report_image),
                modifier = Modifier.size(150.dp),
                onSuccess = {
                    val drawable = it.result.image.asDrawable(context.resources)
                    scope.launch {
                        drawable.toBitmapOrNull()?.toNonHardwareBitmap()?.let { bitmap ->
                            Palette.from(bitmap).generate { palette ->
                                dominantColor =
                                    palette?.getDominantColor(defaultColor.toArgb())?.run {
                                        Color(this)
                                    } ?: defaultColor
                            }
                        }
                    }
                }
            )
            Text(
                text = itemWithPics.item.title, modifier = Modifier.padding(horizontal = 16.dp),
                style = MaterialTheme.typography.titleMedium,
                textAlign = TextAlign.Center,
                fontWeight = FontWeight.Bold
            )
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                Text(
                    text = "$150",
                    style = MaterialTheme.typography.labelMedium,
                    color = Color.Gray,
                    textDecoration = TextDecoration.LineThrough
                )
                Text(
                    text = "$${itemWithPics.item.price}",
                    style = MaterialTheme.typography.bodyLarge,
                    modifier = Modifier.weight(1f),
                    fontWeight = FontWeight.Bold
                )
                Text(text = "${itemWithPics.item.rating}")
                Image(
                    painterResource(R.drawable.star),
                    contentDescription = "star"
                )
            }
        }
    }
}

@Preview
@Composable
fun ProductItemPreview() {
    ProductItem(itemWithPics = testItemWithPics)
}

val testItemEntity = ItemEntity(
    id = R.drawable.img_1_1,
    title = "Agualia Thermal Spa Effect",
    description = "Lorem ipsun dolor sit amet, consectetur adipiscing elit, Sed do eiusnod tenpor incididunt ut labore et dolore magna aliqua.",
    categoryId = 0,
    price = 95,
    rating = 4.6,
    showRecommended = true,
)
val testItemFull = ItemFull(
    item = testItemEntity, pics = List(4) { PicUrlEntity(it, it, "") },
    category = CategoryEntity(0, "")
)
val testItemWithPics = ItemWithPics(
    item = testItemEntity,
    pics = List(4) { PicUrlEntity(it, it, "") },
)